package com.hfjyz.examination.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import com.hfjyz.examination.command.QuestionEditCommand;
import com.hfjyz.examination.domain.QuestionOptions;
import com.hfjyz.examination.service.QuestionService;

public class QuestionEditController extends SimpleFormController {

	private QuestionService questionService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.SimpleFormController#referenceData
	 * (javax.servlet.http.HttpServletRequest, java.lang.Object,
	 * org.springframework.validation.Errors)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		Map model = questionService.initEdit(request,
				(QuestionEditCommand) command, errors);
		return model;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax
	 * .servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
	 * java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		QuestionEditCommand cmd = (QuestionEditCommand) command;
		Integer type = cmd.getQtype();
		Date now = Calendar.getInstance().getTime();
		List<QuestionOptions> qos = new ArrayList<QuestionOptions>();
		switch (type) {
		case 1: // 单选题
			String key = request.getParameter("option1_check");
			List<String> o1 = getParameters(request, "option1_value");
			List<String> oid1 = getParameters(request, "option1_id");
			cmd.setQkey(key);
			for (int i = 0; i < o1.size(); i++) {
				QuestionOptions qo = new QuestionOptions(Long.valueOf(oid1
						.get(i)), null, String.valueOf(i), o1.get(i), now);
				qos.add(qo);
			}
			cmd.setQuestionOptionses(qos);
			break;
		case 2: // 多选题
			String[] keys = request.getParameterValues("option2_check");
			List<String> os2 = getParameters(request, "option2_value");
			List<String> oid2 = getParameters(request, "option2_id");
			cmd.setQkey(StringUtils.arrayToCommaDelimitedString(keys));
			for (int i = 0; i < os2.size(); i++) {
				QuestionOptions qo = new QuestionOptions(Long.valueOf(oid2
						.get(i)), null, String.valueOf(i), os2.get(i), now);
				qos.add(qo);
			}
			break;
		case 3: // 多选题
			String judge = request.getParameter("option3_check");
			cmd.setQkey(judge);
			break;
		}
		questionService.editOk(request, cmd, errors);
		if (errors.hasErrors()) {
			return new ModelAndView(getFormView(), errors.getModel());
		}
		return new ModelAndView(new RedirectView("/categoryList"));
	}

	@SuppressWarnings("unchecked")
	private List<String> getParameters(HttpServletRequest req, String prefix) {
		Map<String, String[]> m = req.getParameterMap();
		List<String> p = new ArrayList<String>();
		for (Map.Entry<String, String[]> entry : m.entrySet()) {
			if (entry.getKey().startsWith(prefix)) {
				p.add(entry.getValue()[0]);
			}
		}
		return p;
	}

	/**
	 * @return the questionService
	 */
	public QuestionService getQuestionService() {
		return questionService;
	}

	/**
	 * @param questionService
	 *            the questionService to set
	 */
	public void setQuestionService(QuestionService categoryService) {
		this.questionService = categoryService;
	}

}
